package com.hongkuncheng.cms.controller.admin;

import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Controller;
import com.hongkuncheng.cms.controller.BaseController;
import com.hongkuncheng.cms.helper.PageHelper;
import com.hongkuncheng.cms.helper.StringHelper;
import com.hongkuncheng.cms.model.AdminRole;
import com.hongkuncheng.cms.model.Form;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 管理员角色管理
 *
 * @author 洪坤成
 */
@Controller
@Transactional
@RequestMapping("admin/adminrole")
public class AadminroleController extends BaseController {

    // 角色列表
    @RequestMapping
    public String index() {
        List<AdminRole> adminroles = factorySession.createCriteria(AdminRole.class)
                .addOrder(Order.asc("sort")).list();
        request.setAttribute("adminroles", adminroles);
        return view();
    }

    // 角色编辑
    @RequestMapping("edit")
    public String edit() {
        int adminroleId = request.getParameter("adminroleid") == null ? 0 :
                Integer.parseInt(request.getParameter("adminroleid"));
        AdminRole adminrole = adminroleId == 0 ? new AdminRole() : factorySession.get(AdminRole.class, adminroleId);
        if (adminroleId == 0) {
            Object uniqueResult = factorySession.createCriteria(AdminRole.class)
                    .setProjection(Projections.max("sort")).uniqueResult();
            int maxSort = uniqueResult == null ? 0 : Integer.parseInt(uniqueResult.toString());
            adminrole.setSort(++maxSort);
        }
        request.setAttribute("adminrole", adminrole);
        List<Form> forms = factorySession.createCriteria(Form.class).addOrder(Order.asc("sort")).list();
        request.setAttribute("forms", forms);
        request.setAttribute("ismaster", Boolean.parseBoolean(applicationIsMaster));
        return view();
    }

    // 角色编辑处理
    @RequestMapping("editdo")
    @ResponseBody
    public void editdo() {
        checkAdminPermission("yunwei_organ_userrole");
        int adminroleId = Integer.parseInt(request.getParameter("adminroleid"));
        String name = request.getParameter("name");
        int exist = Integer.parseInt(factorySession.createCriteria(AdminRole.class)
                .add(Restrictions.eq("name", name))
                .add(Restrictions.ne("id", adminroleId))
                .setProjection(Projections.count("id"))
                .uniqueResult().toString());
        if (exist > 0) {
            PageHelper.alertAndGoBack("角色名称已存在，请重新输入");
            return;
        }
        AdminRole adminrole = adminroleId == 0 ? new AdminRole() :
                factorySession.load(AdminRole.class, adminroleId, LockMode.PESSIMISTIC_WRITE);
        adminrole.setName(name);
        if (adminroleId != 1)
            adminrole.setPermission(StringHelper.join(request.getParameterValues("permission"), ","));
        adminrole.setSort(Integer.parseInt(request.getParameter("sort")));
        if (adminroleId == 0) factorySession.save(adminrole);
        recordLog((adminroleId == 0 ? "新建" : "更新") + "角色《" + adminrole.getName().trim() + "》");
        PageHelper.alertAndRedirect("保存成功", "./");
    }

    // 角色删除
    @RequestMapping("delete")
    @ResponseBody
    public void delete() {
        checkAdminPermission("yunwei_organ_userrole");
        int adminroleId = Integer.parseInt(request.getParameter("adminroleid"));
        AdminRole adminrole = factorySession.load(AdminRole.class, adminroleId, LockMode.PESSIMISTIC_WRITE);
        factorySession.delete(adminrole);
        recordLog("删除角色《" + adminrole.getName().trim() + "》");
        PageHelper.alertAndRedirect("删除成功", "./");
    }

}
